var Common = require('Common');
cc.Class({
    extends: cc.Component,

    properties: {
        ScrollViewEx: require('ScrollViewEx'),
        sprAvatar: cc.Sprite,
        labScore: cc.Label,
        labNick: cc.Label,
        labRank: cc.Label,
        nodNoScore: cc.Node,
        nodMyData: cc.Node,
    },

    onLoad() {
        // this._clearUserCloudStorage();
        // return;
        wx.onMessage(data => {
            if (!data) {
                return;
            }
            if (data.key === 'SHOW_FRIEND_RANK') {
                this._getFriendCloudStorage();
            }
        })

        // kRecord
    },

    start() {
        this.mRankData = {};
        this.myData = {};
    },

    // 清楚用户数据
    _clearUserCloudStorage() {
        wx.removeUserCloudStorage({
            keyList: ['kRecord'],
            success: (res) => {
                console.log('清楚用户数据:success');
            }
        })
    },

    // 获取好友排行榜数据
    _getFriendCloudStorage() {
        wx.getFriendCloudStorage({
            keyList: ['kRecord'],
            success: (res) => {
                // console.log('getFriendCloudStorage:success');
                // console.log(res);
                this._saveData(res.data);

                // console.log('this.mRankData.list');
                // console.log(this.mRankData.list);

                this._showRankList();

                this._getMyData();
            },
            fail: (err) => {
                console.log('_getFriendCloudStorage:error')
                console.log(err)
            }
        })
    },

    // 保存数据
    _saveData(data) {
        this.mRankData.list = [];

        for (let i = 0; i < data.length; i++) {
            if (data[i].KVDataList.length > 0) {
                let item = {};
                item.nick = data[i].nickname;
                item.avatar = data[i].avatarUrl;
                let valueStr = data[i].KVDataList[0].value;
                item.value = JSON.parse(valueStr);
                this.mRankData.list.push(item);
            }
        }

        // 排序
        this.mRankData.list.sort((a, b) => {
            if (a.value.score > b.value.score) {
                return -1;
            }
            return 1;
        })
    },

    _showRankList() {
        var self = this;
        var tData = self.mRankData.list;
        if (!tData || tData.length <= 0) {
            this.nodNoScore.active = true;
            this.nodMyData.active = false;
            return;
        } else {
            this.nodNoScore.active = false;
            this.nodMyData.active = true;
        }

        self.ScrollViewEx.init(tData.length, 7, 2, 3, self._onUpdateItem.bind(self));
    },

    _onUpdateItem(item, idx) {
        var self = this;
        if (!item) {
            return;
        }
        var tComp = item.getComponent("ItemRank");
        if (tComp) {
            var tData = self.mRankData.list;
            var tScore = tData[idx].value.score;
            tComp.init(idx + 1, tData[idx].avatar, tData[idx].nick, tScore);
        }
    },

    // 获取自己的数据
    _getMyData() {
        wx.getUserInfo({
            openIdList: ['selfOpenId'],
            lang: 'zh_CN',
            success: (res) => {
                // console.log('getUserInfo');
                // console.log(res);
                let data = res.data[0];
                if (!data) {
                    return;
                }
                this.myData.nick = data.nickName;
                this.myData.avatar = data.avatarUrl;
                wx.getUserCloudStorage({
                    keyList: ['kRecord'],
                    success: (res) => {
                        // console.log('getUserCloudStorage');
                        // console.log(res);
                        if (res.KVDataList.length > 0) {
                            let valueStr = res.KVDataList[0].value;
                            this.myData.value = JSON.parse(valueStr);
                            this._getMyRank();
                        }
                        this._showMyData();
                    },
                    fail: (err) => {
                        console.log('_getMyData:error')
                        console.log(err)
                    }
                })

            },
            fail: (res) => {
                console.log('getUserInfo:error')
                console.log(err)

            }
        })
    },

    // 获取自己的排行
    _getMyRank() {
        for (let i = 0; i < this.mRankData.list.length; i++) {
            if (this.mRankData.list[i].avatar == this.myData.avatar) {
                this.myData.rank = i + 1;
                return;
            }
        }
    },
    // 更新自己的信息
    _showMyData() {
        Common.loadAvatar(this.sprAvatar, this.myData.avatar);
        this.labRank.string = this.myData.rank || '未上榜';
        this.labNick.string = this.myData.nick;
        if (!this.myData.value) {
            this.labScore.string = 0;
            return;
        }
        this.labScore.string = this.myData.value.score || '0';
    },

    // update (dt) {},
});
